﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;

namespace bmapi.Controllers
{
    [Route("pc")]
    public class PCController : BuyiController
    {
        [HttpPost]
        public HttpResponseMessage ResetPC(dynamic obj)
        {
            return AssistFun.toJson(new {status=200,tip="待完善..." } );
            //if (obj == null)
            //    return AssistFun.toJson(new { status = 201, tip = "参数不能为空" });
            //else
            //{
            //    #region 参数
            //    int pcid = Convert.ToInt32(obj.pcid);
            //    int code = Convert.ToInt32(obj.code);
            //    #endregion
            //    string sql = "select * from PC_tb where pc_id=@pcid";
            //    SQLCommand sc = new SQLCommand(sql,"@pcid",pcid);
            //    DataRow dr = sc.ExecuteOneRow();
            //    if (dr == null)
            //        return NoPC(pcid, code);
            //    else
            //        return HavePC(pcid, code);
            //}
        }
        /// <summary>
        /// 存在流程 修改流程
        /// </summary>
        /// <param name="pcid"></param>
        /// <param name="code"></param>
        private HttpResponseMessage HavePC(int pcid, int code)
        {
            string sql = "update PC_tb set step=@code,station=0,download=0 where pc_id=@pcid";
            SQLCommand sc = new SQLCommand(sql);
            sc.Add("@code",code);
            sc.Add("@pcid",pcid);
            int rtn = sc.ExecuteNonQuery();
            if (rtn == 0)
                return AssistFun.toJson(new { status = 201, tip = "操作失败" });
            else
                return AssistFun.toJson(new { status = 200, tip = "成功" });

        }

        /// <summary>
        /// 不存在流程 创建流程
        /// </summary>
        /// <param name="pc"></param>
        /// <param name="code"></param>
        private HttpResponseMessage NoPC(int pcid,int code)
        {
            SQLCommand sc = null;             
            string sql = "select name,o_model_id,o_PLID from Order_tb where id=@id";
            sc = new SQLCommand(sql,"@id",pcid);
            DataRow dr = sc.ExecuteOneRow();
            if (dr == null)
               return AssistFun.toJson(new { status = 201, tip = "不存在" + pcid + "订单数据" });
            else
            {
                sql = "insert into PC_tb(pc_id,path,PLID,step,maxStep,model_id) values(@pcid,@path,@plid,@step,@maxS,@modelid)";
                sc = new SQLCommand(sql);
                sc.Add("@pcid", pcid);
                sc.Add("@path",dr["name"].ToString());
                sc.Add("@plid", Convert.ToInt32(dr["o_PLID"]));
                sc.Add("@step", code);
                sc.Add("@maxS", code);
                sc.Add("@modelid",Convert.ToInt32(dr["o_model_id"]));
                int rtn = sc.ExecuteNonQuery();
                if (rtn == 0)
                {
                    WriteDebug("NoPC", "新建流程操作失败");
                    return AssistFun.toJson(new { status = 201, tip = "操作失败" });
                }
                else
                {
                    WriteDebug("NoPC", "新建流程操作成功,开始移动文件目录");                   
                    string name = dr["name"].ToString();
                    //移动文件
                    string hisPath = GetHistoryPath(pcid);                 
                    string pcPath = m_buyiRoot+ "\\BuyiWork\\"+name;
                    if (Directory.Exists(hisPath))
                    {
                        Directory.Move(hisPath+"\\"+ name, pcPath);
                        WriteDebug("NoPC", "成功");
                    }
                    return AssistFun.toJson(new { status = 200, tip = "成功" });
                }
            }
            
        }
       
    }
}
